package com.dailyblue.example.springcloud.sentinel.consumer.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dailyblue.example.springcloud.commons.util.JsonResult;
import com.dailyblue.example.springcloud.commons.util.ResultTool;
import com.dailyblue.example.springcloud.sentinel.consumer.feign.SentinelProviderFeign;
import com.dailyblue.example.springcloud.sentinel.consumer.handler.SecondBlockHandler;
import com.dailyblue.example.springcloud.sentinel.consumer.handler.SecondFallback;
import com.dailyblue.example.springcloud.sentinel.consumer.service.SentinelConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class SentinelConsumerServiceImpl implements SentinelConsumerService {
    @Resource
    private SentinelProviderFeign sentinelProviderFeign;

    @Override
    public JsonResult a() {
        return sentinelProviderFeign.a();
    }

    @Override
    public JsonResult b() {
        return sentinelProviderFeign.b();
    }

    @Override
    public JsonResult c() {
        return sentinelProviderFeign.c();
    }

    @SentinelResource("messageD")
    public JsonResult d() {
        return ResultTool.success("This is D");
    }

    @Override
    @SentinelResource(value = "messageG",
            blockHandlerClass = SecondBlockHandler.class, blockHandler = "abc",
            fallbackClass = SecondFallback.class, fallback = "bbb")
    public JsonResult g(int index) {
        log.info("进入了G方法,index:" + index);
        int[] array = {1, 2, 3, 0};
        int result = 8 / array[index];
        log.info("result:{}", result);
        return ResultTool.success("This c");
    }

    @Override
    public JsonResult h(int index) {
        return sentinelProviderFeign.h(index);
    }
    /*
    public JsonResult blockHandler(int index,BlockException e) {
        log.info("进入了blockHandler,e:{}", e);
        return ResultTool.fail(601, "出现异常1");
    }*/

   /* public JsonResult fallback(int index, Throwable e) {
        log.info("进入了fallback,e:{}", e);
        return ResultTool.fail(602, "出现异常2");
    }*/
}
